package com.kirk.cts.ui.view.dialog;

import com.kirk.cts.core.loader.LanguageLoader;
import com.kirk.cts.ui.view.panel.ViewDeatilPanel;
import com.kirk.cts.ui.view.panel.ViewImagePanel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.swing.*;
import java.awt.*;


/**
 * <p> ViewDialog.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-09-01 23:05
 * @Description 查看详细
 */
@Component("viewDialog")
public class ViewDialog extends JDialog {
    private static final long serialVersionUID = 1L;
    // 窗体高度大小
    private static final int FRAME_WIDTH = 650;
    private static final int FRAME_HEIGHT = 550;

    @Resource(name = "viewDeatilPanel")
    private ViewDeatilPanel viewDeatilPanel;

    @Resource(name = "viewImagePanel")
    private ViewImagePanel viewImagePanel;

    /**
     * 内容对话框
     */
//	@Resource(name="contentDialog")
//	private ContentDialog contentDialog;

    private JComponent centerPane;

    private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();

    private int left = (dimensions.width - FRAME_WIDTH) / 2;
    private int top = (dimensions.height - FRAME_HEIGHT) / 2;

    public ViewDialog() {
        setLayout(new BorderLayout());
        //setLocationRelativeTo(contentDialog);
        setSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
        setLocation(left, top);
        setTitle(LanguageLoader.getString("ContentList.contentDetail"));
        setModalityType(ModalityType.APPLICATION_MODAL);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setPreferredSize(getPreferredSize());
    }

    public void setViewSize(int width, int height) {
        setSize(new Dimension(width, height));
    }

    public void showContent(String content) {
        if (!(centerPane instanceof ViewDeatilPanel)) {
            if (null != centerPane) {
                this.remove(centerPane);
            }
            centerPane = viewDeatilPanel;
            add(centerPane);
        }
        viewDeatilPanel.showContent(content);
    }

    public void showImage(String imagePath) {
        if (!(centerPane instanceof ViewImagePanel)) {
            if (null != centerPane) {
                this.remove(centerPane);
            }
            centerPane = viewImagePanel;
            add(centerPane);
        }
        viewImagePanel.showImage(imagePath);
        final ViewDialog viewDialog = this;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Thread currThread = new Thread(viewImagePanel);
                currThread.start();
                Thread waitingThread = new Thread(new WaitingDialog(viewDialog, currThread, LanguageLoader.getString("ViewDetail.loadImage")));
                waitingThread.start();
            }
        });
    }

}
